package com.infore.airsupervise.biz.impl;

import com.infore.airsupervise.app.Constants;
import com.infore.airsupervise.app.MsgConst;
import com.infore.airsupervise.app.StatusConst;
import com.infore.airsupervise.bean.DispatchAqiE;
import com.infore.airsupervise.bean.DispatchSynthesisE;
import com.infore.airsupervise.bean.DispatchWeatherE;
import com.infore.airsupervise.bean.EventE;
import com.infore.airsupervise.biz.DispatchBiz;
import com.infore.airsupervise.http.BaseResponse;
import com.infore.airsupervise.http.HttpAPI;
import com.infore.airsupervise.http.HttpCallBack;
import com.infore.airsupervise.http.HttpUtil;
import com.infore.airsupervise.http.OnServerResponseListener;
import com.infore.airsupervise.utils.JSONUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

/**
 * 实时调度业务接口实现
 * <p>
 * 作者: dingxinlin
 * 创建日期: 2017/8/21 14:37
 */

public class DispatchBizImpl implements DispatchBiz {

    /**
     * 获取实时调度AQI数据
     * @param pageNum
     * @param dataType
     * @param listener
     * @return
     */
    @Override
    public Call<BaseResponse> getDispatchAqiList(
            int pageNum, int dataType, final OnServerResponseListener<List<DispatchAqiE>> listener) {

        if (!HttpUtil.getInstance().isNetworkOk()) {
            listener.onFailed(StatusConst.CODE_CONNECT_FAIL, MsgConst.CONNECT_WRONG, MsgConst.PLEASE_CHECK_NETWORK);
            return null;
        }

        Map<String, Object> params= new HashMap<>();
        params.put("pageNum", pageNum);
        params.put("dataType",dataType);

        Call<BaseResponse> call = HttpUtil.getHttpService().postParams(HttpAPI.GET_DISPATCH_AQI_LIST, params);
        call.enqueue(new HttpCallBack(){
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);

                List<DispatchAqiE> list = JSONUtil.parseList(
                        response.getData(), Constants.RESPONSE_BEAN_LIST_KEY, DispatchAqiE.class);
                listener.onSucceed(list, response.getMessage());

            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode, MsgConst.GET_DATA_FAILED,desc);
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, desc);
                listener.onFailed(errorCode, title, desc);
            }
        });

        return call;
    }

    /**
     * 获取气象数据
     * @param pageNum
     * @param dataType
     * @param listener
     * @return
     */
    @Override
    public Call<BaseResponse> getDispatchWeatherList(
            int pageNum, int dataType, final OnServerResponseListener<List<DispatchWeatherE>> listener) {

        if (!HttpUtil.getInstance().isNetworkOk()) {
            listener.onFailed(StatusConst.CODE_CONNECT_FAIL, MsgConst.CONNECT_WRONG, MsgConst.PLEASE_CHECK_NETWORK);
            return null;
        }

        Map<String, Object> params= new HashMap<>();
        params.put("pageNum", pageNum);
        params.put("dataType",dataType);

        Call<BaseResponse> call = HttpUtil.getHttpService().postParams(HttpAPI.GET_DISPATCH_WEATHER_LIST, params);
        call.enqueue(new HttpCallBack(){
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);

                List<DispatchWeatherE> list = JSONUtil.parseList(
                        response.getData(), Constants.RESPONSE_BEAN_LIST_KEY, DispatchWeatherE.class);
                listener.onSucceed(list, response.getMessage());

            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode, MsgConst.GET_DATA_FAILED,desc);
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, desc);
                listener.onFailed(errorCode, title, desc);
            }
        });

        return call;
    }

    /**
     * 获取综合指数数据
     * @param pageNum
     * @param dataType
     * @param listener
     * @return
     */
    @Override
    public Call<BaseResponse> getDispatchSynthesisList(
            int pageNum, int dataType, final OnServerResponseListener<List<DispatchSynthesisE>> listener) {

        if (!HttpUtil.getInstance().isNetworkOk()) {
            listener.onFailed(StatusConst.CODE_CONNECT_FAIL, MsgConst.CONNECT_WRONG, MsgConst.PLEASE_CHECK_NETWORK);
            return null;
        }

        Map<String, Object> params= new HashMap<>();
        params.put("pageNum", pageNum);
        params.put("dataType",dataType);

        Call<BaseResponse> call = HttpUtil.getHttpService().postParams(HttpAPI.GET_DISPATCH_SYNTHESIS_LIST, params);
        call.enqueue(new HttpCallBack(){
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);

                List<DispatchSynthesisE> list = JSONUtil.parseList(
                        response.getData(), Constants.RESPONSE_BEAN_LIST_KEY, DispatchSynthesisE.class);
                listener.onSucceed(list, response.getMessage());

            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode, MsgConst.GET_DATA_FAILED,desc);
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, desc);
                listener.onFailed(errorCode, title, desc);
            }
        });

        return call;
    }
}
